/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.service;

import com.ddd.context.Parameters;
import com.ddd.entity.Article;
import com.ddd.entity.Journal;
import com.ddd.util.HibernateUtil;
import com.ddd.util.Util;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

public class ArticleService extends BaseService {

    private Parameters para;
    private LogService logService;

    public ArticleService(HttpServletRequest request) {
        para = new Parameters(request);
        logService = new LogService(request);
        this.request = request;
    }

    public Article load(String tjcd) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Article article = (Article) session.load(Article.class, tjcd);
        //Hibernate.initialize(article);

        tx.commit();
        session.close();

        return article;
    }

    public ArrayList loadAll() {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Article.class);
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        para.setTotalSize(result.size());
        return result;
    }

    public void save(Article article) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        article.setCid(para.getUsername());
        article.setCtime(Util.getTodayWithTime());
        session.save(article);
        tx.commit();
        session.close();
    }

    public void update(Article article) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        article.setUid(para.getUsername());
        article.setUtime(Util.getTodayWithTime());
        session.update(article);
        tx.commit();
        session.close();
    }

    public void delete(Article article) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        session.delete(article);
        tx.commit();
        session.close();
    }

    public ArrayList search(String name, String volume, String issue, String year, String publisherID) {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Article.class);

        if (Util.isNotBlank(name)) {
            criteria.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
        }

        if (Util.isNotBlank(volume)) {
            criteria.add(Restrictions.ilike("volume", volume, MatchMode.ANYWHERE));
        }

        if (Util.isNotBlank(issue)) {
            criteria.add(Restrictions.ilike("issue", issue, MatchMode.ANYWHERE));
        }

        if (Util.isNotBlank(year)) {
            criteria.add(Restrictions.ilike("year", year, MatchMode.ANYWHERE));
        }

        // 搜尋限制
        if (Util.isNotBlank(publisherID)) {
            JournalService service = new JournalService(request);
            ArrayList<Journal> list = service.loadByPublisher(publisherID);

            if (list.size() != 0) {
                Disjunction disjunction = Restrictions.disjunction();
                for (Journal j : list) {
                    Criterion journalID = Restrictions.eq("fkJournalId", j.getId());
                    disjunction.add(journalID);
                }
                criteria.add(disjunction);
            } else {
                tx.commit();
                session.close();
                return result;
            }
        }

        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        return result;
    }

    public ArrayList loadByJournal(String journalID) {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Article.class);
        criteria.add(Restrictions.eq("fkJournalId", journalID));
        
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        return result;
    }
}
